package com.bjgzy.courselive.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.bjgzy.courselive.di.component.DaggerCourseLiveCommentComponent;
import com.bjgzy.courselive.mvp.contract.CourseLiveCommentContract;
import com.bjgzy.courselive.mvp.presenter.CourseLiveCommentPresenter;
import com.bjgzy.module_couselive.R;
import com.bjgzy.module_couselive.R2;
import com.blankj.utilcode.util.ObjectUtils;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.entity.CourseListData;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;
import com.umeng.analytics.MobclickAgent;

import butterknife.BindView;
import butterknife.OnClick;
import me.zhanghai.android.materialratingbar.MaterialRatingBar;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * @author xubo
 * @date 2018-8-2 11:43:11
 */
@Route(path = RouterHub.COURSELIVE_COURSELIVECOMMENTACTIVITY)
public class CourseLiveCommentActivity extends BaseActivity<CourseLiveCommentPresenter> implements CourseLiveCommentContract.View {
    @Autowired(name = Constants.ROUTER_DATA)
    CourseListData mData;
    @BindView(R2.id.ratingBar)
    MaterialRatingBar ratingBar;
    @BindView(R2.id.et_comment)
    EditText etComment;
    @BindView(R2.id.btn_submit)
    QMUIRoundButton btnSubmit;
    private QMUITipDialog dialog;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        //如找不到该类,请编译一下项目
        DaggerCourseLiveCommentComponent
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
        return R.layout.courselive_activity_comment;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        btnSubmit.setChangeAlphaWhenPress(true);
    }

    @Override
    public void showLoading() {
        if (dialog == null) {
            dialog = RxUtil.showLoading(getActivity(), "正在提交数据···");
        }
        dialog.show();
    }

    @Override
    public void hideLoading() {
        if (ObjectUtils.isNotEmpty(dialog)) {
            dialog.dismiss();
        }
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public Activity getActivity() {
        return this;
    }


    @OnClick(R2.id.btn_submit)
    public void onViewClicked() {
        if (RxUtil.showEditTip(this, etComment, "请输入您的评论")) {
            return;
        }
        if (ratingBar.getRating() == 0) {
            showMessage("请先对本次进行评分");
            return;
        }
        if (ObjectUtils.isNotEmpty(mData)) {
            String content = etComment.getText().toString();
            int star = (int) ratingBar.getRating();
            MobclickAgent.onEvent(getActivity(),"ttw_course_comment");
            mPresenter.commentPublish(mData.getInternalId(),content,star);
        }
    }
}
